package dao

import (
	"go_server/game_server/database/mysql"
	"go_server/zinx/zlog"
	"go_server/zinx/zutils"
	"gorm.io/datatypes"
	"gorm.io/gorm"
)

type BuildRobotInfo struct {
	RoleId            int64                                           `gorm:"column:roleid;primary"`
	MaxCount          int32                                           `gorm:"column:maxcount"`
	TransVolume       int32                                           `gorm:"column:transvolume"`
	RobotData         datatypes.JSONType[[]string]                    `gorm:"column:robot_data"`
	BuildData         datatypes.JSONType[map[string]string]           `gorm:"column:build_data"`
	BuildResourceData datatypes.JSONType[map[int32]map[int32][]int64] `gorm:"column:build_resource_data"`
}

func (BuildRobotInfo) TableName() string {
	return "buildrobot_info"
}

var (
	all_robotai_info_field = zutils.GetGormFields(BuildRobotInfo{})
)

func CreateBuildRobotInfo(brot *BuildRobotInfo) {
	if brot.RobotData.Data() == nil {
		brot.RobotData = datatypes.NewJSONType(make([]string, 0))
	}

	if brot.BuildData.Data() == nil {
		brot.BuildData = datatypes.NewJSONType(make(map[string]string))
	}

	if brot.BuildResourceData.Data() == nil {
		brot.BuildResourceData = datatypes.NewJSONType(make(map[int32]map[int32][]int64))
	}

	db := mysql.GetServerDBConnection()
	if err := db.Create(brot).Error; err != nil {
		zlog.Errorf("create BuildRobotInfo %v data failed: %v", brot, err)
	}
}

func GetBuildRobotInfo(_roleId int64) *BuildRobotInfo {
	db := mysql.GetServerDBConnection()
	var rstRobot BuildRobotInfo

	if err := db.Select(all_robotai_info_field).Where("roleid=?", _roleId).First(&rstRobot).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			zlog.Errorf("GetBuildRobotInfo ERR!!! id: %s, err: %v", _roleId, err)
		}
		//如果没有机器人数据
		rstRobot = BuildRobotInfo{
			RoleId: _roleId,
		}
		defer CreateBuildRobotInfo(&rstRobot)
	}
	return &rstRobot
}

func UpdateBasicBuildRobotInfo(val *BuildRobotInfo) {
	db := mysql.GetServerDBConnection()
	cols := &map[string]any{"maxcount": val.MaxCount, "transvolume": val.TransVolume}
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", val.RoleId).Updates(cols).Error; err != nil {
		zlog.Errorf("UpdateBasicBuildRobotInfo %v data failed: %v", val, err)
	}
}

func UpdateDataBuildRobotInfo(val *BuildRobotInfo) {
	db := mysql.GetServerDBConnection()
	cols := &map[string]any{"robot_data": val.RobotData, "build_data": val.BuildData}
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", val.RoleId).Updates(cols).Error; err != nil {
		zlog.Errorf("UpdateDataBuildRobotInfo %v data failed: %v", val, err)
	}
}

func UpdateRobotBuildRobotInfo(val *BuildRobotInfo) {
	db := mysql.GetServerDBConnection()
	cols := &map[string]any{"maxcount": val.MaxCount, "transvolume": val.TransVolume, "robot_data": val.RobotData}
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", val.RoleId).Updates(cols).Error; err != nil {
		zlog.Errorf("UpdateRobotBuildRobotInfo %v data failed: %v", val, err)
	}
}

func UpdateBuildRobotInfo_Robot_Data(val *BuildRobotInfo) {
	db := mysql.GetServerDBConnection()
	cols := &map[string]any{"robot_data": val.RobotData}
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", val.RoleId).Updates(cols).Error; err != nil {
		zlog.Errorf("UpdateBuildRobotInfo_Robot_Data %v data failed: %v", val, err)
	}
}

func UpdateBuildRobotInfo_Build_Data(val *BuildRobotInfo) {
	db := mysql.GetServerDBConnection()
	cols := &map[string]any{"build_data": val.BuildData}
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", val.RoleId).Updates(cols).Error; err != nil {
		zlog.Errorf("UpdateBuildRobotInfo_Build_Data %v data failed: %v", val, err)
	}
}

func UpdateBuildRobotInfoByRoleId(RoleId int64, changeField *map[string]any) {
	db := mysql.GetServerDBConnection()
	if err := db.Model(BuildRobotInfo{}).Where("roleid=?", RoleId).Updates(changeField).Error; err != nil {
		zlog.Errorf("UpdateBuildRobotInfoByRoleId %s data failed: %v", RoleId, err)
	}
}
